import { AnimalDatas, GroupTaskData } from '../game/GameDefine';
import { GameData } from '../game/GameData';
import { TaskMgr } from '../game/TaskMgr';
cc.Class({
    extends: require('BasePanel'),

    properties: {
        nodLight: cc.Node,
        sprAnimal: cc.Sprite,
        labName: cc.Label,
        labDiamond: cc.Label,
        nodBtnShare: cc.Node,
        nodBtnAd: cc.Node,
        nodBtnClose: cc.Node,
    },
    onLoad() {
        this._super();

        this.funcCloseBtnClose = () => {
            this.nodBtnClose.active = true;
        }
    },

    onEnter(args, action = true) {
        this._super(args, action);

        this.nodLight.runAction(cc.repeatForever(cc.rotateBy(1, 90)));

        let data = AnimalDatas[args.index];
        let url = 'textures/animals/' + data.icon;
        G.common.setSpriteFrame(this.sprAnimal, url);

        this.labName.string = data.name;
        this.labDiamond.string = 'X' + data.reward;
        this.reward = data.reward;
        GameData.addDiamond(this.reward);

        this.nodBtnClose.active = false;
        this.scheduleOnce(this.funcCloseBtnClose, 3)

        G.partner.sendEvent('解锁', { '类型': '等级' + args.index });

        this._addTask(args.index);
    },

    _addTask(index) {
        for (let i = 0; i < GroupTaskData.length; i++) {
            if ((index + 1) == GroupTaskData[i].taskType) {
                TaskMgr.addGroupTask(i, 1);
            }
        }
    },

    onExit(args) {
        this._super(args);
        this.unschedule(this.funcCloseBtnClose)
    },

    show() {
        this._super();

    },

    hide(cb) {
        this._super(cb);
    },
    onEnable() {

    },
    onDisable() {
    },

    buttonListener: function (button) {
        G.game.gameAudioMgr.playBtn();
        let name = button.node.name;
        if (name == 'btnClose') {
            this.onClose();
        } else if (name == 'btnShare') {


            G.ad.show(G.ad.AD_VIDEO, (param) => {
                G.partner.sendEvent('解锁动物', { '类型': '解锁动物' });
                if (param.event == G.ad.EVENT_SHOW_SUCC) {
                    G.partner.sendEvent('视频广告', { '类型': '解锁动物' });
                    this.onClose();
                    GameData.addDiamond(this.reward);
                    G.game.toast('钻石+' + this.reward, 1);
                    TaskMgr.addDaliyTask(5, 1);
                } else {
                    G.partner.sendEvent('分享', { '类型': '解锁动物' });
                    G.partner.share({ title: '又解锁了一只小动物' }, () => {
                        this.onClose();
                        GameData.addDiamond(this.reward);
                        G.game.toast('钻石+' + this.reward, 1);
                        TaskMgr.addDaliyTask(5, 1);
                    });
                }
            })
        } else if (name == 'btnAd') {
            this.onClose();

        }
    },

    onTouchStart() {

    },
});
